<template>
	<view class="container">
		<form>
			<view class="input-row">
				<view style="width: 220rpx;">巡查工地：</view>
				<picker disabled="true" style="width: 450rpx;text-align: center;" @change="bindProtectChanges" range-key="cn"
					:data-index="22" :data-id="cprojects[cprojectIndex].id" :value="cprojectIndex" :range="cprojects">
					<view class="uni-input">{{cprojects[cprojectIndex].cn}}</view>
					<!-- 还是建议用input保存,可能picker更新不及时 -->
					<input type="text" :value="cprojects[presultindex].id" hidden />
				</picker>
			</view>
			<view class="input-row">
				<view style="width: 220rpx;">巡查模式：</view>
				<picker  style="width: 500rpx;text-align: center;" @change="bindPickerChanges" range-key="cn"
					:data-index="22" :data-id="pModes[index].id" :value="index" :range="pModes">
					<view class="uni-input">{{pModes[index].cn}}</view>
					<!-- 还是建议用input保存,可能picker更新不及时 -->
					<input type="text" :value="pModes[index].id" hidden />
				</picker>
			</view>
			<view class="input-row">
				<view style="width: 220rpx;">巡查类型：</view>
				<picker disabled="true" style="width: 500rpx;text-align: center;" @change="bindptypesChanges" range-key="cn"
					:data-index="22" :data-id="ptypes[ptypeindex].id" :value="ptypeindex" :range="ptypes">
					<view class="uni-input">{{ptypes[ptypeindex].cn}}</view>
					<!-- 还是建议用input保存,可能picker更新不及时 -->
					<input type="text" :value="ptypes[ptypeindex].id" hidden />
				</picker>
			</view>
		<view class="input-row">
			<view style="width: 220rpx;">巡查人2：</view>
			<picker  style="width: 500rpx;text-align: center;" @change="bindpman2sChanges" range-key="cn"
				:data-index="22" :data-id="pman2s[pman2index].id" :value="pman2index" :range="pman2s">
				<view class="uni-input">{{pman2s[pman2index].cn}}</view>
				<input type="text" :value="pman2s[pman2index].id" hidden />
			</picker>
		</view>
			<view class="input-row">
				<view style="width: 220rpx;">周期：</view>
				<input style="width: 500rpx;text-align: center;" type="number" v-model="cycle" />天
			</view>
			<view class="input-row">
				<view style="width: 220rpx;">巡查结果：</view>
				<picker style="width: 500rpx;text-align: center;" @change="bindPresultChanges" range-key="cn"
					:data-index="22" :data-id="presults[presultindex].id" :value="presultindex" :range="presults">
					<view class="uni-input">{{presults[presultindex].cn}}</view>
					<input type="text" :value="presults[presultindex].id" hidden />
				</picker>
			</view>
			<view v-if="ptype=='03'">
				<view class="input-row">
					<view style="width: 220rpx;">建设方负责人：</view>
					<picker style="width: 500rpx;text-align: center;" @change="bindIsexistMcmanChanges" range-key="cn"
						:data-index="22" :data-id="isexist_mcmans[isexist_mcmansindex].id" :value="isexist_mcmansindex" :range="isexist_mcmans">
						<view class="uni-input">{{isexist_mcmans[isexist_mcmansindex].cn}}</view>
						<input type="text" :value="isexist_mcmans[isexist_mcmansindex].id" hidden />
					</picker>
				</view>
				<view class="input-row">
					<view style="width: 220rpx;">施工方负责人：</view>
					<picker style="width: 500rpx;text-align: center;" @change="bindIsexistBcmanChanges" range-key="cn"
						:data-index="22" :data-id="isexist_bcmans[isexist_bcmansindex].id" :value="isexist_bcmansindex" :range="isexist_bcmans">
						<view class="uni-input">{{isexist_bcmans[isexist_bcmansindex].cn}}</view>
						<input type="text" :value="isexist_bcmans[isexist_bcmansindex].id" hidden />
					</picker>
				</view>
				<view class="input-row">
					<view style="width: 220rpx;">监理方负责人：</view>
					<picker style="width: 500rpx;text-align: center;" @change="bindIsexistSvmanChanges" range-key="cn"
						:data-index="22" :data-id="isexist_svmans[isexist_svmansindex].id" :value="isexist_svmansindex" :range="isexist_svmans">
						<view class="uni-input">{{isexist_svmans[isexist_svmansindex].cn}}</view>
						<input type="text" :value="isexist_svmans[isexist_svmansindex].id" hidden />
					</picker>
				</view>
			</view>
			
			<!-- <view style="display: flex;align-items: center;text-align: center;margin-bottom: 30rpx;">
				<button @click="godetail"
					style="width: 500rpx;height: 80rpx;background-color: #ffbb1d;color: #fff;">查看巡查明细</button>
			</view> -->
			<view style="display: flex;align-items: center;text-align: center;">
				<button @click="submitForm"
					style="width: 500rpx;height: 80rpx;background-color: #00aaff;color: #fff;">保存</button>
			</view>
		</form>
	</view>
</template>

<script>
	import {
		request
	} from '@/request/index.js';
	export default {
		data() {
			return {

				cycle:30,
				pphoto1: '',
				pphoto2: '',
				pmode: '',
				pModes: [],
				index: 0,
				ptypes:[],
				ptypeindex:0,
				ptype:'',
				presult: '',
				presults: [],
				presultindex: 0,
				pman2:'',
				pman2s:[],
				pman2index:0,
				cproject: '',
				cprojects: [],
				cprojectIndex: 0,
				project: '',
				userInfo: {},
				detailList: [],
				cprojectId:'',
				isexist_mcmans: [{"cn":"在岗","id":"1"},{"cn":"不在岗","id":"2"}],
				isexist_mcmansindex:0,
				isexist_mcman:'1',
				isexist_bcmans:[{"cn":"在岗","id":"1"},{"cn":"不在岗","id":"2"}],
				isexist_bcmansindex:0,
				isexist_bcman:'1',
				isexist_svmans:[{"cn":"在岗","id":"1"},{"cn":"不在岗","id":"2"}],
				isexist_svmansindex:0,
				isexist_svman:'1'
			};
		},
		onLoad(option) {
			var userInfo = uni.getStorageSync('user');
			if (userInfo == null || userInfo == 'null' || userInfo == '') {
				uni.showToast({
					title: "请先登录",
					success() {
						uni.reLaunch({
							url: "/pages/login/login"
						})
					}
				})

			}
			var _self = this;
			_self.userInfo = userInfo
		
			this.cprojectId = option.cprojectId
			this.getPmode()
			this.getPresults()
			this.getProjects()
			this.getTypes()
			this.getPman2s()

		},
		methods: {
			bindPickerChanges(e) {
				
				this.index = e.target.value
				this.pmode = this.pModes[this.index].id
			},
			bindPresultChanges(e) {
				
				this.presultindex = e.target.value
				this.presult = this.presults[this.presultindex].id
			},
			bindProtectChanges(e) {
				this.cprojectIndex = e.target.value
				this.cproject = this.cprojects[this.cprojectIndex].id
			},
			bindptypesChanges(e){
				this.ptypeindex = e.target.value
				this.ptype = this.ptypes[this.ptypeindex].id
			
			},
			bindpman2sChanges(e){
				this.pman2index = e.target.value
				this.pman2 = this.pman2s[this.pman2index].id
		
			},
			bindIsexistMcmanChanges(e){
				this.isexist_mcmansindex = e.target.value
				this.isexist_mcman = this.isexist_mcmans[this.isexist_mcmansindex].id
			},
			bindIsexistBcmanChanges(e){
				this.isexist_bcmansindex = e.target.value
				this.isexist_bcman = this.isexist_bcmans[this.isexist_bcmansindex].id
			},
			bindIsexistSvmanChanges(e){
				this.isexist_svmansindex = e.target.value
				this.isexist_svman = this.isexist_svmans[this.isexist_svmansindex].id
			},
			removeDetail(index) {
				this.detailList.splice(index, 1);
			},
			addcip() {
				var data = {}
				var that = this
				var location = uni.getStorageSync("location")
				
				// 使用工具函数验证并修正坐标值
				const { validateCoordinates } = require('@/utils/index.js');
				const coords = validateCoordinates(location?.latitude, location?.longitude);
				
				data.longitude = coords.longitude;
				data.latitude = coords.latitude;
				data.cproject = that.cproject
				data.pmode = that.pmode
				data.pman2 = that.pman2
				data.presult = that.presult
				data.ptype = that.ptype
				data.presult = that.presult
				data.cycle = that.cycle
				var uri=""
				if(that.ptype=='01'){
					uri = "/thirdForm/doAdd/c_cpatrol"
				}
				if(that.ptype=='02'){
					uri = "/thirdForm/doAdd/c_spatrol"
				}
				if(that.ptype=='03'){
					uri = "/thirdForm/doAdd/c_qpatrol"
					data.isexist_mcman = that.isexist_mcman
					data.isexist_bcman = that.isexist_bcman
					data.isexist_svman = that.isexist_svman
				}
				request({
					url: uri,
					method: "POST",
					data: data,
					header: {
						'Content-Type': 'application/x-www-form-urlencoded',
						'cookie': uni.getStorageSync('cookie')
					}
				}).then(res => {
					
					if (res.data.code == '200') {
						uni.redirectTo({
							url:"/pages/cpatrol2/cpatrol2?cprojectId="+this.cproject
						})
						
						// uni.showModal({
						// 	title:"提示",
						// 	content:"保存成功，是否继续添加巡查明细",
						// 	showCancel:true,
						// 	success:async(resp) => {
						// 		console.log(resp)
						// 		var data = res.data.data
						// 		var pid = JSON.parse(data).pid
						// 		if(resp.confirm){
						// 			uni.redirectTo({
						// 				url:"/pages/sipdetail/sipdetail?pid="+pid
						// 			})
						// 		}else{
						// 			uni.redirectTo({
						// 				url:'/pages/cpatrol/cpatrol'
						// 			})
						// 		}
						// 	}
						// })
					} else {
						uni.showToast({
							title: res.data.msg,
							
						})
					}
				})
			},

			getPmode() {
				var that = this
				request({
					url: "/thirdWidget/comboJson/c_cpatrol-pmode",
					method: "POST",
					header: {
						'Content-Type': 'application/x-www-form-urlencoded',
						'cookie': uni.getStorageSync('cookie')
					}
				}).then(res => {
					
					if (res.data.code == '200') {
						that.pModes = []
						for (var i = 0; i < res.data.data.length; i++) {

							var mode = {}
							mode.id = res.data.data[i].id
							mode.cn = res.data.data[i].cn
							if (mode.id != '') {
								that.pModes.push(mode)
							}

						}
						that.pmode = that.pModes[that.index].id
					} else {
						uni.showToast({
							title: res.data.msg,
							
						})
					}
				})
			},
			getTypes() {
				var that = this
				request({
					url: "/thirdWidget/comboJson/c_cpatrol-ptype",
					method: "POST",
					header: {
						'Content-Type': 'application/x-www-form-urlencoded',
						'cookie': uni.getStorageSync('cookie')
					}
				}).then(res => {
					
					if (res.data.code == '200') {
						that.ptypes = []
			// 			for (var i = 0; i < res.data.data.length; i++) {
			
			// 				var mode = {}
			// 				mode.id = res.data.data[i].id
			// 				mode.cn = res.data.data[i].cn
			// 				if (mode.id != '') {
			// 					that.ptypes.push(mode)
			// 				}
						
			// 				if(this.userInfo.dept.deptName=='市场科'&&mode.id=='01'){
			// 					that.ptypeindex = i
			// 				}
			// 				if(this.userInfo.dept.deptName=='安全科'&&mode.id=='02'){
			// 					that.ptypeindex = i
			// 				}
			// 				if(this.userInfo.dept.deptName=='质监科'&&mode.id=='03'){
			// 					that.ptypeindex = i
			// 				}
			// // console.log(this.userInfo.dept.deptName)
			// // console.log(mode.id)
			// // console.log(that.ptypeindex)
			// 			}
					var mode = {}
				
						if(this.userInfo.dept.deptName=='市场科'){
							mode.id = '01'
							mode.cn='文明施工巡查'
						}
						if(this.userInfo.dept.deptName=='安全科'){
							mode.id = '02'
							mode.cn='安全监督巡查'
						}
						if(this.userInfo.dept.deptName=='质监科'){
							mode.id = '03'
							mode.cn='质量监督巡查'
						}
						that.ptypes.push(mode)
						that.ptypeindex = 0
						that.ptype = that.ptypes[that.ptypeindex].id
					} else {
						uni.showToast({
							title: res.data.msg,
							
						})
					}
				})
			},
			getPresults() {
				var that = this
				request({
					url: "/thirdWidget/comboJson/c_cpatrol-presult",
					method: "POST",
					header: {
						'Content-Type': 'application/x-www-form-urlencoded',
						'cookie': uni.getStorageSync('cookie')
					}
				}).then(res => {
					
					if (res.data.code == '200') {
						that.presults = []
						for (var i = 0; i < res.data.data.length; i++) {

							var result = {}
							result.id = res.data.data[i].id
							result.cn = res.data.data[i].cn
							if (result.id != '') {
								that.presults.push(result)
							}

						}
						that.presult = that.presults[that.presultindex].id

					} else {
						uni.showToast({
							title: res.data.msg,
							
						})
					}
				})
			},
			
			getPman2s() {
				var that = this
				request({
					url: "/thirdWidget/findJson?code=c_cpatrol&field=pman2",
					method: "POST",
					header: {
						'Content-Type': 'application/x-www-form-urlencoded',
						'cookie': uni.getStorageSync('cookie')
					}
				}).then(res => {
				
					if (res.data.success) {
						that.pman2s = []
						for (var i = 0; i < res.data.rows.length; i++) {
							var result = {}
							result.id = res.data.rows[i].id
							result.cn = res.data.rows[i].nickname
							if (result.id != '') {
								that.pman2s.push(result)
							}
			
						}
						that.pman2 = that.pman2s[that.pman2index].id
			
					} else {
						uni.showToast({
							title: res.data.msg,
							
						})
					}
				})
			},
			getProjects() {
				var that = this
				request({
					url: "/thirdWidget/findJson?code=c_cpatrol&field=cproject&rows=2000",
					method: "POST",
					header: {
						'Content-Type': 'application/x-www-form-urlencoded',
						'cookie': uni.getStorageSync('cookie')
					}
				}).then(res => {
					
					if (res.data.success == true) {
						that.cprojects = []
						for (var i = 0; i < res.data.rows.length; i++) {
							var project = {}
							project.id = res.data.rows[i].cpid
							project.cn = res.data.rows[i].csname
							that.cprojects.push(project)
							if(project.id == that.cprojectId){
								that.cprojectIndex = i
							}
						}
						that.cproject = that.cprojects[that.cprojectIndex].id

					} else {
						uni.showToast({
							title: res.data.msg,
							
						})
					}
				})
			},
			submitForm() {
				// 这里可以添加提交表单的逻辑
				this.addcip()
			},
			godetail() {
				uni.redirectTo({
					url: "/pages/sipdetail/sipdetail"
				})
			},
			chooseImg1() {
				var that = this
				uni.chooseImage({
					count: 1, // 默认9，设置图片的数量
					sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						console.log(tempFilePaths)
						that.pphoto1 = tempFilePaths[0]
						const uploadTask = uni.uploadFile({
							url: 'https://qscs.qtech2000.cn:8443/thirdUpload/img?name=pphoto1_file', // 开发者服务器的URL
							filePath: tempFilePaths[0],
							name: 'pphoto1_file', // 必填，文件对应的 key
							header: {
								'cookie': uni.getStorageSync('cookie')
							},
							success: (uploadFileRes) => {
								console.log('upload success:', uploadFileRes);
								that.pphoto1 = uploadFileRes.data.data.fileName
							},
							fail: (uploadFileErr) => {
								console.error('upload fail:', uploadFileErr);
							}
						});
						// 监听上传进度变化
						uploadTask.onProgressUpdate((res) => {
							console.log('上传进度' + res.progress + '%');
						});
					},
					fail: (chooseImageErr) => {
						console.error('choose image fail:', chooseImageErr);
					}
				});
			},
			chooseImg2() {
				var that = this
				uni.chooseImage({
					count: 1, // 默认9，设置图片的数量
					sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						console.log(tempFilePaths)
						that.pphoto2 = tempFilePaths[0]
						const uploadTask = uni.uploadFile({
							url: 'https://qscs.qtech2000.cn:8443/thirdUpload/img?name=pphoto2_file', // 开发者服务器的URL
							filePath: tempFilePaths[0],
							name: 'pphoto2_file', // 必填，文件对应的 key
							success: (uploadFileRes) => {
								console.log('upload success:', uploadFileRes);
								that.pphoto2 = uploadFileRes.data.data.fileName
							},
							fail: (uploadFileErr) => {
								console.error('upload fail:', uploadFileErr);
							}
						});
						//监听上传进度变化
						uploadTask.onProgressUpdate((res) => {
							console.log('上传进度' + res.progress + '%');
						});
					},
					fail: (chooseImageErr) => {
						console.error('choose image fail:', chooseImageErr);
					}
				});
			}
		},

	};
</script>

<style>
	.container {
		padding: 20px;
	}

	.input-row {
		margin-bottom: 20px;
		display: flex;

	}

	.input-row text {
		flex-grow: 1;
		margin-right: 10px;
	}
</style>